﻿
using KinonekoSoftware.Studio.Models.Shells;

namespace KinonekoSoftware.Studio.ViewModels
{
    public partial class AppViewModel
    {
        private readonly IMessageBus<JuXiaoYouNotification> _notifyQueue;

        private async void OnNotificationProc(JuXiaoYouNotification msg)
        {
            if (msg is DatabaseDumpNotification)
            {
                //
                //
                await DumpAsync();
            }
            else if (msg is DatabaseModeSwitchNotification)
            {
                var runtime   = Ioc.Get<RepositoryRuntime>();
                var setting   = Ioc.Get<JuXiaoYouSetting>();
                var workspace = runtime.Repository?.Workspace;

                if (string.IsNullOrEmpty(workspace))
                {
                    await Danger(TitleSR.DangerOperation, "切换世界观失败");
                    return;
                }

                //
                // 切换数据模式
                if (setting.DataMode != DataMode.Release)
                {
                    setting.DataMode = DataMode.Release;
                    await Platform.SaveSetting(setting);
                }


                await runtime.CloseAsync();
                await runtime.LoadAsync(workspace, DataMode.Release);

            }
            else if (msg is DatabaseBackupNotification)
            {
                var runtime = Ioc.Get<RepositoryRuntime>();
                await runtime.BackupAsync();
            }
            else if (msg is UpdateCheckNotification updateMSG)
            {
#if WINDOWS
                var url = updateMSG.Urls
                              .FirstOrDefault(x => !string.IsNullOrEmpty(x));

                
                Platform.OpenLink(url);

#endif
            }
        }

        public async Task DumpAsync()
        {
            var runtime = Ioc.Get<RepositoryRuntime>();

            //
            //
            await runtime.DumpAsync();

            await Success(TitleSR.SuccessOperation, TextSR.Successful_Save);
        }
    }
}